App = {
  web3Provider: null,
  contracts: {},

  init: function () {
    return App.initWeb3();
  },

  initWeb3: function () {
    // Initialize web3 and set the provider to the testRPC.
    if (typeof web3 !== 'undefined') {
      App.web3Provider = web3.currentProvider;
      web3 = new Web3(web3.currentProvider);
    } else {
      // set the provider you want from Web3.providers
      App.web3Provider = new Web3.providers.HttpProvider('http://127.0.0.1:7545');
      web3 = new Web3(App.web3Provider);
    }

    return App.initContract();
  },

  initContract: function () {
    $.getJSON('Token.json', function (data) {
      // Get the necessary contract artifact file and instantiate it with truffle-contract.
      var TutorialTokenArtifact = data;
      App.contracts.TutorialToken = TruffleContract(TutorialTokenArtifact);

      // Set the provider for our contract.
      App.contracts.TutorialToken.setProvider(App.web3Provider);

      // Use our contract to retieve and mark the adopted pets.
      return App.getBalances();
    });

    return App.bindEvents();
  },

  bindEvents: function () {
    $(document).on('click', '#transferButton', App.handleTransfer);
  },

  handleTransfer: function (event) {
    event.preventDefault();

    var amount = parseInt($('#TTTransferAmount').val());
    var toAddress = $('#TTTransferAddress').val();

    console.log('Transfer ' + amount + ' MT to ' + toAddress);

    var tutorialTokenInstance;

    web3.eth.getAccounts(function (error, accounts) {
      if (error) {
        console.log(error);
      }

      var account = accounts[0];

      App.contracts.TutorialToken.deployed().then(function (instance) {
        tutorialTokenInstance = instance;

        return tutorialTokenInstance.transfer(toAddress, amount, { from: account, gas: 100000 });
      }).then(function (result) {
        alert('Transfer Successful!');
        let recipt = web3.eth.getTransactionReceipt(result.tx, (err, sets) => {
          if (!err) console.log('getTransactionReceipt result==>:', JSON.stringify(sets));
        });
        console.log('transfer result:', JSON.stringify(result));

        return App.getBalances();
      }).catch(function (err) {
        console.log(err.message);
      });
    });
  },

  getBalances: function () {
    console.log('Getting balances...');

    var tutorialTokenInstance;

    web3.eth.getAccounts(function (error, accounts) {
      if (error) {
        console.log(error);
      }

      var account = accounts[0];

      App.contracts.TutorialToken.deployed().then(async function (instance) {
        tutorialTokenInstance = instance;


        return ({balance:await tutorialTokenInstance.balanceOf(account),symbol:await tutorialTokenInstance.symbol.call()});
      }).then(function (result) {
        // balance = result.c[0];

        $('#TTBalance').text(result.balance+' '+result.symbol);
      }).catch(function (err) {
        console.log(err.message);
      });
    });
  }

};

$(function () {
  $(window).load(function () {
    App.init();
  });
});
